<template>
 <div class="channel-edit">
    <van-cell title="我的频道" :border="false">
      <van-button
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
      >{{isEdit ? '完成' : '编辑'}}</van-button>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="(channel, index) in channels"
        :key="index"
        :text="channel.name"
        @click="onMyChannelFn(index, channel.id)"
      >
        <span 
          class="text"
          :class="{active: active === index}" 
          slot="text">
          {{ channel.name }}
        </span>
        <van-icon v-show="isEdit && index !== 0" slot="icon" name="close" />
      </van-grid-item>
    </van-grid> 
    <van-cell title="频道推荐" :border="false"></van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="channel-item"
        v-for="value in recommendChannels"
        :key="value.id"
        :text="value.name"
        icon="plus"
        @click="addToChannelsFn(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels, 
  addUserChannel,
  deleteUserChannel
} from '@/api/channel'
import {mapState} from 'vuex'
import {setItem} from '@/utils/storage'

export default {
  name: 'ChannelEdit',
  props: {
    channels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      allChannels: [],
      isEdit: false
    }
  },
  created() {
    this.getAllChannelsFn()
  },
  methods: {
    async getAllChannelsFn() {
      try {
        const {data} = await getAllChannels()

        this.allChannels = data.data.channels
      } catch(err) {
        console.log(err)
      }
    },
    async addToChannelsFn(val) {
      this.channels.push(val)

      if(this.user) {
        try {
          await addUserChannel({
            id: val.id,
            seq: this.channels.length
          })

          this.$toast('添加成功')
        } catch(err) {
          this.$toast('添加错误')
        }

      } else {
        setItem('TOUTIAO_CHANNEL', this.channels)
      }
    },
    onMyChannelFn(index, id) {
      if(this.isEdit) {
        if(index === 0) {
          return
        }

        if(index <= this.active) {
          this.$emit('updateActive', this.active - 1, true)
        }

        this.channels.splice(index, 1)

        this.deleteChannel(index, id, this.channels)
      } else {
        this.$emit('updateActive', index, false)
      }

    },
    async deleteChannel(index, id, channels) {
      if(this.user) {
        try {
          await deleteUserChannel(id)
        } catch(err) {
          this.$toast('删除失败')
        }
      } else {
        setItem('TOUTIAO_CHANNEL', channels)
      }
    }
  },
  computed: {
    recommendChannels() {
      return this.allChannels.filter(item => !this.channels.find(item2 => item2.id === item.id))
    },
    ...mapState(['user'])
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  /deep/ .van-grid-item__icon-wrapper {
    position: unset;
  }
  .channel-item {
    height: 86px;
    /deep/ .van-grid-item__content {
      background-color: #f5f5f6;
      width: 160px;
      .van-grid-item__text {
        color: #222;
        font-size: 28px;
        margin-top: 0;
      }
    }
  }

  /deep/ .active{
    color: red!important;;
  }
  /deep/ .text {
    color: #222;
    font-size: 28px;
    white-space: nowrap;
  }

  /deep/ .recommend-grid {
    .van-grid-item__content {
        flex-direction: row;
        color: #222;
        .van-icon {
            font-size: 24px;
        }

        .van-grid-item__text {
            font-size: 28px;
            margin-top: 0;
        }
    }
  }

  /deep/ .van-icon-close {
    position: absolute;
    right: -10px;
    top: -10px;
    font-size: 36px;
    color: #ccc;
  }
}
</style>
